function [errors] = fcn_calculate_errors(reference, data, no_int)
%fcn_calculate_errors   This function calculates errors between reference and
%                       acquired flight data.

%   [errors] = fcn_calculate_errors(reference, data)
%   This function calculates errors between reference and acquired flight
%   data from the simulations.

%   IN      reference   reference flight data
%   IN      data        data to be compared to reference
%   IN      no_int      Number of samples before position integration starts (time for the algorithm to reach a steady state)
%   OUT     errors      calculated errors

% Position errors
errors.dX = reference.X - data.X;
errors.dY = reference.Y - data.Y;    
errors.dZ = reference.Z - data.Z;
errors.dpos = sqrt(errors.dX.^2 + errors.dY.^2 + errors.dZ.^2);
errors.dpos_mean = mean(errors.dpos);
errors.dpos_after_noint_mean = mean(errors.dpos((no_int+1):end));

% Velocity error
errors.dvel = reference.vel - data.vel;

% Euler angles error
errors.dphi = reference.phi - data.phi;
errors.dphi_mean = mean(errors.dphi);
errors.dtheta = reference.theta - data.theta;
errors.dtheta_mean = mean(errors.dtheta);
errors.dpsi = reference.psi - data.psi;
errors.dpsi_mean = mean(errors.dpsi);
% Euler angle errors when starting position integration
errors.dphi_int = reference.phi(no_int+1) - data.phi(no_int+1);
errors.dtheta_int = reference.theta(no_int+1) - data.theta(no_int+1);
errors.dpsi_int = reference.psi(no_int+1) - data.psi(no_int+1);

% Quaternion attitude error
errors.dq = reference.q - data.q;
% Quaternion attitude error when starting position integration
errors.dq_int = reference.q(no_int+1,:) - data.q(no_int+1,:);

end